﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfeccionesJR.Entites;
using System.Data;
using System.Data.Objects;

namespace ConfeccionesJR.Repository
{
    public class DetalleVentaRepository : MasterDataAccess, IDetalleVentaRepository
    {
       
        public List<DETALLEVENTA> GetFromDetalleVentaByProductoDescAndVentaId(string descripcion, int id)
        {
            if (descripcion != "")
            {
                var query = from c in Context.DETALLEVENTAs.Include("PRODUCTO")
                            where (c.PRODUCTO.Descripcion.ToUpper().Contains(descripcion.ToUpper())) & (c.VentaId.Equals(id))
                            select c;
                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
            else
            {
                var query = from c in Context.DETALLEVENTAs.Include("PRODUCTO")
                            where c.VentaId.Equals(id)
                            select c;
                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
        }

        public DETALLEVENTA GetDetalleVentaByProductoIdAndVentaId(int idProd, int idVenta)
        {
            return Context.DETALLEVENTAs.Include("PRODUCTO").Where(c => c.PRODUCTO.Id.Equals(idProd) && c.VentaId.Equals(idVenta)).SingleOrDefault();
        }

        public void RemoveDetalleVentaByProductoIdAndVentaId(int idVenta, int idProd)
        {
            var existe = Context.DETALLEVENTAs.Where(c => c.VentaId.Equals(idVenta) && c.PRODUCTO.Id.Equals(idProd) ).SingleOrDefault();
            Context.DETALLEVENTAs.Remove(existe);
            Context.SaveChanges();
        }

        public bool GetAllDetallesVentaByVentaId(int idVenta)
        {
            var query = from c in Context.DETALLEVENTAs.Include("VENTA")
                        where c.VentaId.Equals(idVenta)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.Any();
        }
    }
}
